package com.lxx.modules.sys.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;
import com.lxx.modules.sys.biz.ISysLogBiz;
import com.lxx.common.BaseAction;
import com.lxx.modules.sys.entity.SysLog;
import com.lxx.utils.Constant;
import com.lxx.utils.DataTableResult;
import com.lxx.utils.NetworkUtil;
import com.lxx.utils.PageInfo;
import com.lxx.utils.Pagination;
import com.lxx.utils.Result;
import com.lxx.annotation.PermissionName;

@Controller
@RequestMapping("/sysLog")
public class SysLogController extends BaseAction{
	private static final Logger LOGGER = LoggerFactory.getLogger(SysLogController.class);
	
	@Autowired
	private ISysLogBiz sysLogBiz;
	
	@RequestMapping("/query")
	@ResponseBody
	@PermissionName("查询")
	public Result query(PageInfo pageInfo, HttpServletRequest request) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String beginStr = request.getParameter("begin");
		String endStr = request.getParameter("end");
		Map<String, Object> params = new HashMap<>();
		try {
			if (StringUtils.isNotBlank(beginStr)) {
				params.put("begin", format.parse(beginStr));
			}
			if (StringUtils.isNotBlank(endStr)) {
				params.put("end", format.parse(endStr));
			}
			params.put("keywords", request.getParameter("keywords"));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		int pageIndex = pageInfo.getStart() / pageInfo.getLength() + 1;
		Pagination<SysLog> pagination = sysLogBiz.query(params, pageIndex, pageInfo.getLength());
		DataTableResult dataTableResult = new DataTableResult(pageInfo.getDraw(), 
				pagination.getTotalRow(), pagination.getTotalRow(), pagination.getDatas());
		return Result.success(dataTableResult);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	@PermissionName("新增")
	public Result add(SysLog sysLog) {
		LOGGER.debug("{}", sysLog);
		sysLog.setDelFlag(false);
		sysLog.setCreateTime(new Date());
		sysLogBiz.save(sysLog);
		return Result.SUCCESS;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	@PermissionName("修改")
	public Result update(SysLog sysLog) {
		sysLogBiz.update(sysLog);
		return Result.SUCCESS;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	@PermissionName("删除")
	public Result delete(String ids) {
		sysLogBiz.delete(ids);
		return Result.SUCCESS;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	@PermissionName("详情")
	public Result detail(Integer id) {
		SysLog sysLog = sysLogBiz.detail(id);
		return Result.success(sysLog);
	}

}
